let mongoose=require('./db')

let Userschema=new mongoose.Schema({
    name:String,//用户名
    password:String,//密码
    telp:String,//电话
    head:{
        type:String,
        default:''
    },//头像
    background:String,//背景图
    faceImage:String,//人脸图片
})
// 刷新令牌模型
const RefreshToken = mongoose.model('aaa', {
    token: String,
    userId: mongoose.Schema.Types.ObjectId,
    createdAt:{
        type:Number,
        default:Date.now()}
})

// 新增聊天记录模型
let chatRecordSchema = new mongoose.Schema({
    from: String,
    to: String,
    message: String,
    type:{
        type:String,
        default:'text'
    },
    timestamp: {
        type: Date,
        default: Date.now
    }
});
let chatRecordModel = mongoose.model('chatRecord', chatRecordSchema, "chatRecord");

let GoodsSchema = new mongoose.Schema({
    productCode: { // 商品编码（唯一）
        type: String,
        required: true,
        unique: true
    },
    productName: { // 商品名称
        type: String,
        required: true
    },
    price: { // 商品价格
        type: Number,
        required: true,
        min: 0 // 价格不能为负数
    },
    listingDate: { // 上架日期
        type: Date,
        default: Date.now // 默认当前时间
    },
    status: { // 商品状态
        type: String,
        enum: ["已发布", "已下架"], // 限定两种状态
        default: "已发布" // 默认状态为已发布
    }
})

let GoodsModel = mongoose.model('Goods', GoodsSchema, "Goods")

let OrderSchema = new mongoose.Schema({
    productCode: { // 订单编码（唯一）
        type: String,
        required: true,
        unique: true
    },
    productName: { // 订单名称
        type: String,
        required: true
    },
    price: { // 订单价格
        type: Number,
        required: true,
        min: 0 // 价格不能为负数
    },
    listingDate: { // 订单时间
        type: Date,
        default: Date.now // 默认当前时间
    },
    status: { // 订单状态
        type: String,
        enum: ["已完成", "待发货","待支付"], // 限定三种状态
        default: "待支付" // 默认状态为待支付
    },
    pay: { // 付款类型
        type: String,
        enum: ["微信", "支付宝"], 
        default: "微信",
        required: false  // 允许空值
    }
})

let OrderModel = mongoose.model('Order', OrderSchema, "Order")

let InvoiceSchema = new mongoose.Schema({
    productCode: { // 订单编码（唯一）
        type: String,
        required: true,
        unique: true
    },
    productName: { // 商品名称
        type: String,
        required: true
    },
    price: { // 单价
        type: Number,
        required: true,
        min: 0 // 价格不能为负数
    },
    num: { //数量
        type: Number,
        required: true,
        min: 0 //数量不能为负数
    },
})
let InvoiceModel = mongoose.model('Invoice', InvoiceSchema, "Invoice")
let user=mongoose.model("loginuser",Userschema,"loginuser")
let  ContactsSchema = new mongoose.Schema({
    id:String,
      name:String,//用户名
    telp:String,//电话
    head:{
        type:String,
        default:""
    },//头像
    bz:{type:String,default:""},//备注
    is:{type:Boolean,default:false},
});
let  ContactsModel = mongoose.model('Contacts', ContactsSchema, "Contacts");


module.exports={
     user,
    RefreshToken,
    chatRecordModel,
       GoodsModel,
        OrderModel,
        InvoiceModel,
        ContactsModel
}